package com.cho.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SQLiteConnection {

	public final static String DATABASE = "crystalcube.db";

	static {
		try {
			Class.forName("org.sqlite.JDBC");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection("jdbc:sqlite:sqlAnalysis.db");
		} catch (SQLException e) {
			// TMIS Auto-generated catch block
			e.printStackTrace();
		}
		
		return conn;
	}
	
	
	
	/*
	public boolean open() {
	    try {
	        // 읽기 전용
	        SQLiteConfig config = new SQLiteConfig();
	        config.setReadOnly(true);
	        this.connection = DriverManager.getConnection("jdbc:sqlite:/" + this.dbFileName, config.toProperties());
	    } catch(SQLException e) { e.printStackTrace(); return false; }
	 
	    isOpened = true;
	    return true;
	}
	 
	 
	public boolean close() {
	    if(this.isOpened == false) { return true; }
	 
	    try {
	        this.connection.close();
	    } catch(SQLException e) { e.printStackTrace(); return false; }
	    return true;
	}

	public boolean readMeta(String filePath, String hashCode, PCloudItem item) throws SQLException {
	    if(this.isOpened == false) { return false; }
	 
	    boolean result = false;
	    String query = "SELECT * FROM media WHERE FilePath=? AND CheckSum=?;";
	    PreparedStatement prep = 
	        this.connection.prepareStatement(query);
	    prep.setString(1, filePath);
	    prep.setString(2, hashCode);
	 
	    ResultSet row = prep.executeQuery();
	    if(row.next()) {
	        row.getString(1);           // index 로 가져오기
	        row.getString("FileSize");  // field 이름으로 가져오기
	         
	        result = true;
	    }
	    row.close();
	    prep.close();
	    return result;
	}
	*/
}
